﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BUS.DAL
{
    public class DatHangDAL
    {
        internal static int Insert(DatHangBUS datHangBUS)
        {
            var db = DataAccess.Database;

            DatHang dh = new DatHang()
            {
                IDKhachHang = datHangBUS.KhachHang.ID,
                ThoiGianDatHang = DateTime.Now,
                TrangThai = 0
            };

            db.DatHangs.InsertOnSubmit(dh);
            db.SubmitChanges();

            return dh.ID;
        }

        internal static List<DatHangBUS> GetListWaiting()
        {
            ShopDataContext db = DataAccess.Database;

            var result = from p in db.DatHangs
                         where p.TrangThai == 0
                         select new DatHangBUS()
                         {
                             ID = p.ID,
                             KhachHang = new KhachHangBUS()
                             {
                                 ID = p.KhachHang.ID,
                                 HoTen = p.KhachHang.HoTen,
                                 SoDienThoai = p.KhachHang.SoDienThoai,
                                 DiaChi = p.KhachHang.DiaChi,
                                 CMND = p.KhachHang.CMND,
                                 Email = p.KhachHang.Email
                             },
                             ThoiGianDatHang = (DateTime)p.ThoiGianDatHang,
                             TrangThai = (int)p.TrangThai,
                             ChiTietDatHangs = ChiTietDatHangDAL.GetByDatHang(p.ID)
                         };

            return result.ToList<DatHangBUS>();
        }

        internal static int UpdateDuyet(DatHangBUS datHangBUS)
        {
            var db = DataAccess.Database;

            var result = db.DatHangs.SingleOrDefault<DatHang>(p => p.ID == datHangBUS.ID);
            if (result != null)
            {
                result.TrangThai = (short)datHangBUS.TrangThai;
                db.SubmitChanges();
                return (short)result.TrangThai;
            }
            return 0;
        }

        internal static List<DatHangBUS> GetListChecked()
        {
            ShopDataContext db = DataAccess.Database;

            var result = from p in db.DatHangs
                         where p.TrangThai == 1
                         orderby p.KhachHang.ID descending
                         select new DatHangBUS()
                         {
                             ID = p.ID,
                             KhachHang = new KhachHangBUS()
                             {
                                 ID = p.KhachHang.ID,
                                 HoTen = p.KhachHang.HoTen,
                                 SoDienThoai = p.KhachHang.SoDienThoai,
                                 DiaChi = p.KhachHang.DiaChi,
                                 CMND = p.KhachHang.CMND,
                                 Email = p.KhachHang.Email
                             },
                             ThoiGianDatHang = (DateTime)p.ThoiGianDatHang,
                             TrangThai = (short)p.TrangThai,
                             ChiTietDatHangs = ChiTietDatHangDAL.GetByDatHang(p.ID)
                         };

            return result.ToList<DatHangBUS>();
        }

        internal static void Delete(DatHangBUS datHangBUS)
        {
            var db = DataAccess.Database;

            var dhDel = db.DatHangs.SingleOrDefault<DatHang>(p => p.ID == datHangBUS.ID);

            if (dhDel != null) db.DatHangs.DeleteOnSubmit(dhDel);
            db.SubmitChanges();
        }
        #region d
        internal static List<DatHangBUS> GetAll()
        {
            ShopDataContext db = new ShopDataContext();

            var result = from p in db.DatHangs
                         orderby p.ThoiGianDatHang descending
                         select new DatHangBUS()
                         {
                             ID = p.ID,
                             KhachHang = new KhachHangBUS()
                             {
                                 ID = p.KhachHang.ID,
                                 HoTen = p.KhachHang.HoTen,
                                 SoDienThoai = p.KhachHang.SoDienThoai,
                                 DiaChi = p.KhachHang.DiaChi,
                                 CMND = p.KhachHang.CMND,
                                 Email = p.KhachHang.Email
                             },
                             ThoiGianDatHang = (DateTime)p.ThoiGianDatHang,
                             TrangThai = (short)p.TrangThai,
                             ChiTietDatHangs = ChiTietDatHangDAL.GetByDatHang(p.ID)
                         };

            return result.ToList<DatHangBUS>();
        }
        internal static DatHangBUS GetByID(int ID)
        {
             ShopDataContext db = new ShopDataContext();
            var result = from p in db.DatHangs
                         where p.ID==ID
                         select new DatHangBUS()
                         {
                             ID = p.ID,
                             KhachHang = new KhachHangBUS()
                             {
                                 ID = p.KhachHang.ID,
                                 HoTen = p.KhachHang.HoTen,
                                 SoDienThoai = p.KhachHang.SoDienThoai,
                                 DiaChi = p.KhachHang.DiaChi,
                                 CMND = p.KhachHang.CMND,
                                 Email = p.KhachHang.Email
                             },
                             ThoiGianDatHang = (DateTime)p.ThoiGianDatHang,
                             TrangThai = (short)p.TrangThai,
                             ChiTietDatHangs = ChiTietDatHangDAL.GetByDatHang(p.ID)
                         };

            return result.FirstOrDefault<DatHangBUS>();
        }
        internal static void updateTrangThai(DatHangBUS datHangBUS)
        {
            ShopDataContext db = new ShopDataContext();
            DatHang dh = db.DatHangs.Single(p => p.ID == datHangBUS.ID);
            dh.TrangThai = (short)datHangBUS.TrangThai;
            db.SubmitChanges();
        }
        internal static void Xoa(DatHangBUS datHangBUS)
        {
            ShopDataContext db = new ShopDataContext();
            DatHang dh = db.DatHangs.Single(p => p.ID == datHangBUS.ID);
            db.ChiTietDatHangs.DeleteAllOnSubmit(dh.ChiTietDatHangs);
            db.SubmitChanges();

            TaiKhoan tk = db.TaiKhoans.Single(p => p.SoTaiKhoan == dh.SoTaiKhoan);
            tk.SoTien += datHangBUS.ThanhTien;
            db.DatHangs.DeleteOnSubmit(dh);
            db.SubmitChanges();
        }
        #endregion





       
    }
}
